<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 推广项目
 */
class Project extends JS_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('project_mdl');
		if($this->session->suser->group != 1){
			show_error('参数错误！');
		}
	}


	public function index(){
		$this->db->order_by('sort desc,id desc');
		$list = $this->project_mdl->select();
		$data = array(
			'list' => $list
			);
		$this->_view('project_index',$data);
	}

	/**
	 * 项目排序
	 */
	public function set_sort(){
		if(!$this->input->is_ajax_request()){
			show_404();
		}

		$pid = $this->input->post('pid',TRUE);
		$pid = intval($pid);
		if(empty($pid)){
			die('参数错误！');
		}

		$val = $this->input->post('val',TRUE);
		$val = intval($val);

		$this->db->where('id',$pid);
		$res = $this->project_mdl->update(array('sort'=>$val));
		echo $res === true ? '1' : '修改失败！';
	}

	/**
	 * 推广项目修改
	 */
	public function update(){
		$pid = $this->input->get('pid',TRUE);
		$pid = intval($pid);
		if(empty($pid)){
			show_error('参数错误！');
		}
		$this->db->where('id',$pid);
		$res = $this->project_mdl->get_one();

		if(empty($res)){
			show_error('该数据不存在');
		}

		$this->_view('project_update',array('res'=>$res));
	}

	/**
	 * 项目修改
	 */
	public function operation(){
		if(!$this->input->is_ajax_request()){
			show_404();
		}

		$post = $this->input->post(NULL,TRUE);
		if(empty($post['id'])){
			die('参数错误！');
		}

		//验证
		if(empty($post['p_name'])){
			die('项目名称不能为空！');
		}
		if(empty($post['price']) || intval($post['price']) <= 0){
			die('项目单价为大于0的整数');
		}
		
		$data = array(
			'p_name' => $post['p_name'],
			'price' => intval($post['price']),  
			'status' => intval($post['status']),  
			);

		$this->db->where('id',intval($post['id']));
		$res = $this->project_mdl->update($data);

		echo $res === true ? '1' : '修改失败！';
	}

}

/* End of file Project.php */
/* Location: ./application/controllers/Project.php */